<?php

namespace app\index\model;
use think\facade\Db;
use app\common\model\Common;

class CardsTran extends Common
{
    /**
     * 返回layui的table所需要的格式
     * @param $post
     * @param $user_id
     * @return mixed
     * @throws \think\db\exception\DbException
     */
    public function tableDataIndex($post)
    {
        $list_rows  = $post['limit'] ?? config('paginate.list_rows');
        $page       = $post['page'] ?? config('paginate.page');

        $where =[];
        $where[] = ['u.id', '=', $post['user_id']];
        if(isset($post['card_id']) && !empty($post['card_id'])){
            $where[] = ['c.id', '=', $post['card_id']];
        }
        if(isset($post['tran_type']) && !empty($post['tran_type'])){
            $where[] = ['ct.tran_type', '=', $post['tran_type']];
        }
        if(isset($post['card_num']) && !empty($post['card_num'])){
            $where[] = ['c.card_num', 'like', '%'.$post['card_num'].'%'];
        }

        if(isset($post['datetime']) && !empty($post['datetime'])){
            $time_list = explode('_' , $post['datetime']);
            $start_time = strtotime($time_list[0] . " 00:00:00") - 13*60*60;
            $end_time   = strtotime($time_list[1] . " 23:59:59") - 13*60*60;
            $where[] = ['ct.ctime', 'between', "{$start_time},{$end_time}"];
        }

        $list = Db::name('cards_tran')->alias('ct')
            ->leftJoin('cards c','c.card_token=ct.card_token')
            ->leftJoin('user u' , 'u.id=c.user_id')
            ->field('u.username,c.first_name,c.last_name,c.card_num,ct.*')
            ->order('ct.ctime desc')
            ->where($where)
            ->paginate(['list_rows' => $list_rows, 'page' => $page])
            ->toArray();
        $list_data = $list['data'];
        if(isset($list_data) && !empty($list_data)){
            foreach ($list_data as &$v){
                if($v['tran_type'] == "CREDIT"){
                    $v['amount'] = - $v['amount'];
                    $v['amount_real'] = - $v['amount_real'];
                }
            }
        }
        return $result = [
            'code'  => 0,
            'msg'   => lang('get info success'),
            'data'  => $list_data,
            'count' => $list['total'],
        ];
    }

    public function getUserPay(){
        $list = Db::name('cards_trans')->alias('ct')
                ->leftJoin('cards c' , 'c.card_num=ct.card_num')
                ->field('c.card_num,ct.ctime,ct.amount_auth,ct.shopping')
                ->order('ct.ctime desc')
                ->where("ct.tran_status='Authorized'")
                ->limit("20")->select()
                ->toArray();
        if($list){
            foreach ($list as $k=>&$v){
                $v['card_num'] = substr($v['card_num'] , -4);
            }
        }
        return $list;
    }

    /**
     * 返回layui的table所需要的格式
     * @param $post
     * @param $user_id
     * @return mixed
     * @throws \think\db\exception\DbException
     */
    public function getList($post)
    {
        $list_rows  = $post['limit'] ?? config('paginate.list_rows');
        $page       = $post['page'] ?? config('paginate.page');

        $where ="ct.type='authorization' and ct.state='PENDING'";
        if(isset($post['desc']) && !empty($post['desc'])){
            $where .= "  and c.card_num like '%{$post['desc']}%' or ct.desc like '%{$post['desc']}%'";
        }

        if(isset($post['datetime']) && !empty($post['datetime'])){
            $time_list = explode('_' , $post['datetime']);
            $start_time = strtotime($time_list[0] . " 00:00:00") - 8*60*60;
            $end_time   = strtotime($time_list[1] . " 23:59:59") - 8*60*60;
            $where .= " and (ct.ctime between {$start_time} and {$end_time})";
        }

        $list = Db::name('cards_transactions')->alias('ct')
            ->leftJoin('cards c' , 'c.card_token=ct.card_token')
            ->field('c.card_num,ct.desc,ct.ctime,ct.amount')
            ->order('ct.ctime desc')
            ->where($where)
            ->paginate(['list_rows' => $list_rows, 'page' => $page])
            ->toArray();
        return $result = [
            'code'  => 0,
            'msg'   => lang('get info success'),
            'data'  => $list['data'],
            'count' => $list['total'],
        ];
    }
}